/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dpy.appmvdpy.Controllers;

import com.dpy.appmvdpy.Facades.ClientesFacade;
import com.dpy.appmvdpy.Facades.DefaultFacade;
import com.dpy.appmvdpy.Facades.UsersFacade;
import com.dpy.appmvdpy.Facades.VendedorFacade;
import com.dpy.appmvdpy.Models.Clientes;
import com.dpy.appmvdpy.Models.Exporters.ClientExporter;
import com.dpy.appmvdpy.Models.Exporters.Exporter;
import com.dpy.appmvdpy.Models.Importers.ClientImporter;
import com.dpy.appmvdpy.Models.Importers.Importer;
import com.dpy.appmvdpy.Models.Importers.VendedorImporter;
import com.dpy.appmvdpy.Models.Users;
import com.dpy.appmvdpy.Models.Validators.ClientesValidator;
import com.dpy.appmvdpy.Models.Validators.DefaultValidator;
import com.dpy.appmvdpy.Models.Validators.VendedorValidator;
import com.dpy.appmvdpy.Models.Vendedor;
import java.beans.PropertyEditorSupport;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author ac_psoares
 */
@Controller("requestscoped")
@RequestMapping("/vendedores")
@Secured("ROLE_ADMIN")
public class VendedorController extends DefaultController<Vendedor> {

    @Autowired
    public VendedorFacade facade;
    
    @Autowired
    public VendedorValidator validator;
    
    @Autowired
    public UsersFacade userFacade;
    
    @Autowired
    public VendedorImporter importer;
    
    
    public VendedorController()
    {
        super(Clientes.class,"vendedores");
    }
       
    @Override
    public DefaultFacade<Vendedor> getFacade() {
        return this.facade;
    }
    
    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Users.class, "userId", new PropertyEditorSupport() {

            @Override
            public String getAsText() {
                return getValue() == null ? "" : ((Users) getValue()).getId().toString();
            }

            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                Users user = userFacade.getById(Integer.valueOf(text));
                setValue(user);
            }
        });
        
    }

    @Override
    public Vendedor createObject() {
        return new Vendedor();
    }

    @Override
    public DefaultValidator<Vendedor> getValidator() {
        return this.validator;
    }

    @Override
    @Secured("ROLE_ADMIN")
    public ModelAndView list(@RequestParam(required=false, defaultValue="0") int pag) {
        return super.list(pag);
    }

    @Override
    @Secured("ROLE_ADMIN")
    public ModelAndView create() {
        ModelAndView model = super.create();
        model.addObject("userList",userFacade.listAll());
        return model;
    }

    @Override
    @Secured("ROLE_ADMIN")
    public ModelAndView edit(int id) {
        ModelAndView model = super.edit(id);
        model.addObject("userList",userFacade.listAll());
        return model;
    }  
    
     @Override
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ModelAndView download(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return super.download(request, response);
    }

    @Override    
    public Importer<Vendedor> getImporter(File file) {
        return new VendedorImporter(file);
    }  
    
    @Override
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ModelAndView Import() {
        return super.Import();
    }
    
}
